-- use_property
-- Created by chengb Oct/20/2015
-- 成就：使用道具

-- 内部函数声明
local whenUseProperty;

-- 注册玩家使用道具的回调
EventMgr.register("gamecenter_achievement/use_property", event.USE_PROPERTY, function(args)
    whenUseProperty(args);
end);

-- 注册玩家使用道具的回调
whenUseProperty = function(args)
    local classId = args.classId;

    -- 遍历所有的子规则
    local ids = GameCenterAchievementM.getIdsByRule("use_property");

    -- 判断是否是符合要求的成就
    local function isValidId(id)
        if true == GameCenterAchievementM.isCompleted(id) then
            -- 已经完成了
            return false;
        end

        -- 判断是否是需求道具
        local config = GameCenterAchievementM.query(id);
        local ruleArg = config["rule_arg"];
        if type(ruleArg) == 'table' then
            if type(ruleArg["class_id"]) == 'number' and 
                ruleArg["class_id"] ~= classId then
                return false;
            end
            
            if type(ruleArg["class_id"]) == 'table' and 
                table.indexOf(ruleArg["class_id"], classId) == -1 then
                return false;
            end
        end

        return true;
    end

    for _, id in pairs(ids) do
        if isValidId(id) then
            -- 尝试完成成就
            GameCenterAchievementM.tryComplete(id, 1);
        end
    end
end

